// ==UserScript==
// @name           bro3_put_trade_text.user.js
// @namespace      http://ronf-bro3.googlecode.com/
// @include        http://*.3gokushi.jp/card/trade.php*
// @include        http://*.1kibaku.jp/card/trade.php*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @require        http://flot.googlecode.com/svn/trunk/jquery.flot.js
// @description    ブラウザ三国志トレード情報テキスト生成
// @version        0.02
// ==/UserScript==
// version date       author
// ver0.01 2011.05.12 ronf 実装テスト
// ver0.02 2011.05.22 ronf パラメータが表に出てしまうのを修正＋documentを利用している部分を修正

var VERSION_KEY = "000ptt";
var VERSION = "0.01";
var urlpath;
var maxpage;
var putText = "";
var card;

// 共通関数
var d = document;
var $d = function(id) { return d.getElementById(id); };
var $x = function(xp,dc) { return d.evaluate(xp, dc||d, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; };
var $e = function(key) { return d.evaluate(key, d, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); };

//----------------//
// メインルーチン //
//----------------//
(function(){
	if (location.pathname == "/card/trade.php") {
		// 合成履歴画面のみ動作

		//HTML追加
		addHtml();
	}
})();

//------------//
// HTMLの生成 //
//------------//
function addHtml() {

	//--------------------------------------//
	// 画面生成                             //
	//--------------------------------------//

	//----------------//
	// コンテナの取得 //
	//----------------//
	var container;
	var pager = $e('//*[@class="pager"]');
	var bbs = $e('//*[@id="gray02Wrapper"]');
	if (pager.snapshotLength == 0) {
		container = bbs.snapshotItem(0);
	} else {
		container = pager.snapshotItem(0);
	}
	

	//----------------//
	//-- ヘッダー部 --//
	//----------------//
	//-- コントロール配置DIV --//
	var formE = d.createElement("form");
	container.appendChild(formE);
	var linksDiv = d.createElement("div");
	formE.appendChild(linksDiv);

	//-- ツール名称ラベル --//
	var textLabel = d.createElement("span");
	textLabel.id = "toolLabel";
	textLabel.style.fontSize = "16px";
	textLabel.innerHTML = "<br><b>トレード情報抽出 Ver." + VERSION + "</b>";
	textLabel.style.color = "black";
	textLabel.style.width = "725px";
	linksDiv.appendChild(textLabel);

	//-- (改行) --//
	textLabel = d.createElement("span");
	textLabel.innerHTML = "<br>";
	linksDiv.appendChild(textLabel);

	//-- ページ数表示 --//
	var ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "16px";
	ckLabel.innerHTML = "トレードページ数";
	linksDiv.appendChild(ckLabel);

	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.id = "pageNo";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "0";
	linksDiv.appendChild(ckLabel);

	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "/";
	linksDiv.appendChild(ckLabel);

	var data = $e('//ul[@class="pager"]/li[@class="last"]//a[@title="last page"]');
	if( data.snapshotLength > 0 ){
		var ref = data.snapshotItem(0).getAttribute("href");
		var matches = ref.match(/p=(\d+)/);
		maxpage = matches[1];
		urlpath = ref.replace(/\?p=[\d]+/,"\?p=TARGETPAGENO");
	}
	else{
		var data = $e('//ul[@class="pager"]/li/b');
		if( data.snapshotLength > 0 ){
			maxpage = data.snapshotItem(0).textContent;

			var data = $e('//ul[@class="pager"]/li//a[@title="first page"]');
			var ref = data.snapshotItem(0).getAttribute("href");
			urlpath = ref.replace(/\?p=[\d]+/,"\?p=TARGETPAGENO");
		}
		else{
			maxpage = 1;
		}
	}

	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.id = "lastPageNo";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = maxpage;
	linksDiv.appendChild(ckLabel);
	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "&nbsp;";
	linksDiv.appendChild(ckLabel);
	var check1 = d.createElement("input");
	check1.type = "checkbox";
	check1.id = "ckCheckText";
	check1.name = "output_text";
	check1.checked = true;
	linksDiv.appendChild(check1);
	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "テキスト";
	linksDiv.appendChild(ckLabel);
	var check2 = d.createElement("input");
	check2.type = "checkbox";
	check2.id = "ckCheckTsv";
	check2.name = "output_tsv";
	check2.checked = true;
	linksDiv.appendChild(check2);
	ckLabel = d.createElement("span");
	ckLabel.style.fontSize = "14px";
	ckLabel.style.marginLeft = "4px";
	ckLabel.innerHTML = "TSV";
	linksDiv.appendChild(ckLabel);

	//-- 情報取得ボタン --//
	var button1 = d.createElement("input");
	button1.type = "button";
	button1.id = "ckButton1";
	button1.style.fontSize = "12px";
	button1.style.marginLeft = "16px";
	button1.value = "トレード情報を抽出";
	button1.addEventListener("click", function() {getButtonClicked()}, true);
	linksDiv.appendChild(button1);

	//-- 結果全選択ボタン --//
	var button2 = d.createElement("input");
	button2.type = "button";
	button2.id = "ckButton2";
	button2.style.fontSize = "12px";
	button2.style.marginLeft = "16px";
	button2.value = "結果全選択";
	button2.addEventListener("click", function() {selectButtonClicked()}, true);
	linksDiv.appendChild(button2);

	//-- (改行) --//
	textLabel = d.createElement("span");
	textLabel.innerHTML = "<br>";
	linksDiv.appendChild(textLabel);

	// （ギャップ埋め）
	ckLabel = d.createElement("pre");
	ckLabel.style.height = "6px";
	linksDiv.appendChild(ckLabel);

	//----------------------------//
	// 検索結果のテキスト表示領域 //
	//----------------------------//
	textArea = d.createElement("div");
	textArea.innerHTML = "";
	textArea.style.fontSize = "14px";
	textArea.style.color = "black";
	textArea.style.backgroundColor = "#FFDEAD";
	textArea.style.width = "725px";
	textArea.style.height = "200px";
	textArea.style.overflow = "auto";
	textArea.style.border = "solid 2px";
	textArea.style.padding = "2px";
	textArea.style.marginTop = "-2px";
	textArea.style.textAlign = "left";
	linksDiv.appendChild(textArea);

	ckLabel = d.createElement("pre");
	ckLabel.id = "resultText";
	ckLabel.innerHTML = "";
	textArea.appendChild(ckLabel);

	//--------------------------//
	// 他ページ取得情報の設定域 //
	//--------------------------//
	var linksDiv2 = d.createElement("html");
	linksDiv2.id = "hiddenData";
	linksDiv2.style.display = "none";
	container.appendChild(linksDiv2);
}


//------------------------//
// 取得ボタン押下処理処理 //
//------------------------//
function getButtonClicked() {
	// ハッシュテーブル初期化
	card = new Array();

	var textBox = $e('//*[@id="resultText"]');
	textBox.snapshotItem(0).innerHTML = "データ取得中・・・「終了しました」のダイアログが出るまでお待ち下さい";

	var button = $e('//*[@id="ckButton1"]');
	button.snapshotItem(0).disabled = true;
	button = $e('//*[@id="ckButton2"]');
	button.snapshotItem(0).disabled = true;

	// 見出し
	putText = "カードNo.\tレアリティ\t名前\tレベル\tスコア\t落札希望価格\t入札数\t強制公開期限<br>";

	if( maxpage > 1 ){
		loadPage(1);
	}
	else{
		// 2ページ目以降
		var data = $e('//table[@class="tradeTables"]//td');
		var text = "";
		
		var nextlimit;
		if ((new Date()).getHours() < 10) {
			nextlimit = getDateString();
		} else {
			nextlimit = getDateString(1);
		}
		for( var i = 0; i < data.snapshotLength/9; i++ ){
			var limit = data.snapshotItem(i*9+7).textContent.replace(/(?:^\s+)|(?:\s+$)/g, "");
			if (limit != nextlimit && limit != '---') continue;

			var res = "";
			var no = data.snapshotItem(i*9+0).textContent.replace(/(?:^\s+)|(?:\s+$)/g, "");
			var matches;
			matches = data.snapshotItem(i*9+1).textContent.match(/^\s*([A-Z]+)\s*([^A-Z\s]+)/);
			var rare = matches[1];
			var name = matches[2];
			var lv = parseInt(data.snapshotItem(i*9+2).textContent.replace(/(?:^\s+)|(?:\s+$)/g, ""), 10);
			var score = parseInt(data.snapshotItem(i*9+4).textContent.replace(/(?:^\s+)|(?:\s+$)/g, ""), 10);
			var price = parseInt(data.snapshotItem(i*9+5).textContent.replace(/(?:^\s+)|(?:\s+$)/g, "").replace(',', ''), 10);
			var count = parseInt(data.snapshotItem(i*9+6).textContent.replace(/(?:^\s+)|(?:\s+$)/g, ""), 10);

			if( card[no] == undefined ){
				card[no] = [];
				card[no]['no'] = no;
				card[no]['rare'] = rare;
				card[no]['name'] = name;
				card[no]['tradecount'] = 1;
				
				card[no]['nextlimit'] = {};
				card[no]['nextlimit']['bid'] = {
					all: {maxprice: null, tradecount: 0},
					sl0: {maxprice: null, tradecount: 0}
				};
				card[no]['nextlimit']['nobid'] = {
					all: {minprice: null, tradecount: 0},
					sl0: {minprice: null, tradecount: 0}
				};
				card[no]['nobid'] = {
					all: {minprice: null, tradecount: 0},
					sl0: {minprice: null, tradecount: 0}
				};

				if (limit == '---') {
					card[no]['nobid']['all']['tradecount'] = 1;
					card[no]['nobid']['all']['minprice'] = price;
					if (score == 0 && lv == 0) {
						card[no]['nobid']['sl0']['tradecount'] = 1;
						card[no]['nobid']['sl0']['minprice'] = price;
					}
				} else if (count == 0) {
					card[no]['nextlimit']['nobid']['all']['tradecount'] = 1;
					card[no]['nextlimit']['nobid']['all']['minprice'] = price;
					if (score == 0 && lv == 0) {
						card[no]['nextlimit']['nobid']['sl0']['tradecount'] = 1;
						card[no]['nextlimit']['nobid']['sl0']['minprice'] = price;
					}
				} else {
					card[no]['nextlimit']['bid']['all']['tradecount'] = 1;
					card[no]['nextlimit']['bid']['all']['maxprice'] = price;
					if (score == 0 && lv == 0) {
						card[no]['nextlimit']['bid']['sl0']['tradecount'] = 1;
						card[no]['nextlimit']['bid']['sl0']['maxprice'] = price;
					}
				}
			} else{
				++card[no]['tradecount'];
				if (limit == '---') {
					++card[no]['nobid']['all']['tradecount'];
					if (card[no]['nobid']['all']['minprice'] == null || card[no]['nobid']['all']['minprice'] > price) card[no]['nobid']['all']['minprice'] = price;
					if (score == 0 && lv == 0) {
						++card[no]['nobid']['sl0']['tradecount'];
						if (card[no]['nobid']['sl0']['minprice'] == null || card[no]['nobid']['sl0']['minprice'] > price) card[no]['nobid']['sl0']['minprice'] = price;
					}
				} else if (count == 0) {
					++card[no]['nextlimit']['nobid']['all']['tradecount'];
					if (card[no]['nextlimit']['nobid']['all']['minprice'] == null || card[no]['nextlimit']['nobid']['all']['minprice'] > price) card[no]['nextlimit']['nobid']['all']['minprice'] = price;
					if (score == 0 && lv == 0) {
						++card[no]['nextlimit']['nobid']['sl0']['tradecount'];
						if (card[no]['nextlimit']['nobid']['sl0']['minprice'] == null || card[no]['nextlimit']['nobid']['sl0']['minprice'] > price) card[no]['nextlimit']['nobid']['sl0']['minprice'] = price;
					}
				} else {
					++card[no]['nextlimit']['bid']['all']['tradecount'];
					if (card[no]['nextlimit']['bid']['all']['maxprice'] == null || card[no]['nextlimit']['bid']['all']['maxprice'] < price) card[no]['nextlimit']['bid']['all']['maxprice'] = price;
					if (score == 0 && lv == 0) {
						++card[no]['nextlimit']['bid']['sl0']['tradecount'];
						if (card[no]['nextlimit']['bid']['sl0']['maxprice'] == null || card[no]['nextlimit']['bid']['sl0']['maxprice'] < price) card[no]['nextlimit']['bid']['sl0']['maxprice'] = price;
					}
				}
			}
			text = text + no + "\t" + rare + "\t" + name + "\t" + lv + "\t" + score + "\t" + price + "\t" + count + "\t" + limit + "<br>";
		}

		var textBox = $e('//*[@id="resultText"]');
		var output = '';
		if ($d('ckCheckText').checked) {
			var analyze = makeAnalyzeText();
			output += analyze + '<BR><BR>' + putText + text + "<BR><BR><BR>";
		}
		if ($d('ckCheckTsv').checked) {
			var tsv = makeAnalyzeTsv();
			output += tsv +'<BR>';
		}
		textBox.snapshotItem(0).innerHTML = output;

		var button = $e('//*[@id="ckButton1"]');
		button.snapshotItem(0).disabled = false;
		button = $e('//*[@id="ckButton2"]');
		button.snapshotItem(0).disabled = false;

		alert("終了しました");
	}
}

//----------------------//
// ページのローディング //
//----------------------//
function loadPage(targetPageNo) {
	var opt = {
		method: 'get',
		url: urlpath.replace('TARGETPAGENO', targetPageNo),
		onload: function(res) {
			addResult(targetPageNo, res);
		}
	}
	GM_xmlhttpRequest(opt); 
}

//--------------//
// 取得結果加算 //
//--------------//
function addResult(targetPageNo,res) {
	var pageNo = $e('//*[@id="pageNo"]');
	pageNo.snapshotItem(0).textContent = targetPageNo;

	var hidden = $e('//*[@id="hiddenData"]');
	hidden.snapshotItem(0).innerHTML = res.responseText;

	// 2ページ目以降
	var data = $e('//*[@id="hiddenData"]//table[@class="tradeTables"]//td');
	var text = "";
	
	var nextlimit;
	if ((new Date()).getHours() < 10) {
		nextlimit = getDateString();
	} else {
		nextlimit = getDateString(1);
	}
	for( var i = 0; i < data.snapshotLength/9; i++ ){
		var limit = data.snapshotItem(i*9+7).textContent.replace(/(?:^\s+)|(?:\s+$)/g, "");
		if (limit != nextlimit && limit != '---') continue;

		var res = "";
		var no = data.snapshotItem(i*9+0).textContent.replace(/(?:^\s+)|(?:\s+$)/g, "");
		var matches;
		matches = data.snapshotItem(i*9+1).textContent.match(/^\s*([A-Z]+)\s*([^A-Z\s]+)/);
		var rare = matches[1];
		var name = matches[2];
		var lv = parseInt(data.snapshotItem(i*9+2).textContent.replace(/(?:^\s+)|(?:\s+$)/g, ""), 10);
		var score = parseInt(data.snapshotItem(i*9+4).textContent.replace(/(?:^\s+)|(?:\s+$)/g, ""), 10);
		var price = parseInt(data.snapshotItem(i*9+5).textContent.replace(/(?:^\s+)|(?:\s+$)/g, "").replace(',', ''), 10);
		var count = parseInt(data.snapshotItem(i*9+6).textContent.replace(/(?:^\s+)|(?:\s+$)/g, ""), 10);

		if( card[no] == undefined ){
			card[no] = [];
			card[no]['no'] = no;
			card[no]['rare'] = rare;
			card[no]['name'] = name;
			card[no]['tradecount'] = 1;
			
			card[no]['nextlimit'] = {};
			card[no]['nextlimit']['bid'] = {
				all: {maxprice: null, tradecount: 0},
				sl0: {maxprice: null, tradecount: 0}
			};
			card[no]['nextlimit']['nobid'] = {
				all: {minprice: null, tradecount: 0},
				sl0: {minprice: null, tradecount: 0}
			};
			card[no]['nobid'] = {
				all: {minprice: null, tradecount: 0},
				sl0: {minprice: null, tradecount: 0}
			};

			if (limit == '---') {
				card[no]['nobid']['all']['tradecount'] = 1;
				card[no]['nobid']['all']['minprice'] = price;
				if (score == 0 && lv == 0) {
					card[no]['nobid']['sl0']['tradecount'] = 1;
					card[no]['nobid']['sl0']['minprice'] = price;
				}
			} else if (count == 0) {
				card[no]['nextlimit']['nobid']['all']['tradecount'] = 1;
				card[no]['nextlimit']['nobid']['all']['minprice'] = price;
				if (score == 0 && lv == 0) {
					card[no]['nextlimit']['nobid']['sl0']['tradecount'] = 1;
					card[no]['nextlimit']['nobid']['sl0']['minprice'] = price;
				}
			} else {
				card[no]['nextlimit']['bid']['all']['tradecount'] = 1;
				card[no]['nextlimit']['bid']['all']['maxprice'] = price;
				if (score == 0 && lv == 0) {
					card[no]['nextlimit']['bid']['sl0']['tradecount'] = 1;
					card[no]['nextlimit']['bid']['sl0']['maxprice'] = price;
				}
			}
		} else{
			++card[no]['tradecount'];
			if (limit == '---') {
				++card[no]['nobid']['all']['tradecount'];
				if (card[no]['nobid']['all']['minprice'] == null || card[no]['nobid']['all']['minprice'] > price) card[no]['nobid']['all']['minprice'] = price;
				if (score == 0 && lv == 0) {
					++card[no]['nobid']['sl0']['tradecount'];
					if (card[no]['nobid']['sl0']['minprice'] == null || card[no]['nobid']['sl0']['minprice'] > price) card[no]['nobid']['sl0']['minprice'] = price;
				}
			} else if (count == 0) {
				++card[no]['nextlimit']['nobid']['all']['tradecount'];
				if (card[no]['nextlimit']['nobid']['all']['minprice'] == null || card[no]['nextlimit']['nobid']['all']['minprice'] > price) card[no]['nextlimit']['nobid']['all']['minprice'] = price;
				if (score == 0 && lv == 0) {
					++card[no]['nextlimit']['nobid']['sl0']['tradecount'];
					if (card[no]['nextlimit']['nobid']['sl0']['minprice'] == null || card[no]['nextlimit']['nobid']['sl0']['minprice'] > price) card[no]['nextlimit']['nobid']['sl0']['minprice'] = price;
				}
			} else {
				++card[no]['nextlimit']['bid']['all']['tradecount'];
				if (card[no]['nextlimit']['bid']['all']['maxprice'] == null || card[no]['nextlimit']['bid']['all']['maxprice'] < price) card[no]['nextlimit']['bid']['all']['maxprice'] = price;
				if (score == 0 && lv == 0) {
					++card[no]['nextlimit']['bid']['sl0']['tradecount'];
					if (card[no]['nextlimit']['bid']['sl0']['maxprice'] == null || card[no]['nextlimit']['bid']['sl0']['maxprice'] < price) card[no]['nextlimit']['bid']['sl0']['maxprice'] = price;
				}
			}
		}
		text = text + no + "\t" + rare + "\t" + name + "\t" + lv + "\t" + score + "\t" + price + "\t" + count + "\t" + limit + "<br>";
	}

	if(maxpage > targetPageNo){
		putText = putText + text;
		loadPage(parseInt(targetPageNo)+1);
	}
	else{

		var textBox = $e('//*[@id="resultText"]');
		var output = '';
		if ($d('ckCheckText').checked) {
			var analyze = makeAnalyzeText();
			output += analyze +
						    '<BR>トレード情報<BR>' +
						    '----------------------------------------------------------<BR>' +
						    putText + text +
						    '----------------------------------------------------------<BR><BR><BR>';

		}
		if ($d('ckCheckTsv').checked) {
			var tsv = makeAnalyzeTsv();
			output += tsv +'<BR>';
		}
		textBox.snapshotItem(0).innerHTML = output;


		var button = $e('//*[@id="ckButton1"]');
		button.snapshotItem(0).disabled = false;
		button = $e('//*[@id="ckButton2"]');
		button.snapshotItem(0).disabled = false;

		alert("終了しました");
	}
}

//--------------//
// 全選択ボタン //
//-------------//
function selectButtonClicked() {
	// 構築ルート情報の選択
	var textBox = $x('//*[@id="resultText"]');

	var objs = textBox.firstChild;
	var obje = textBox.lastChild;

	var range = d.createRange();
	range.setStart(objs,0);
	range.setEnd(obje,obje.textContent.length);
	var sel = getSelection();
	sel.removeAllRanges();
	sel.addRange(range);
}


//--------------//
// 分析テキスト //
//-------------//
function makeAnalyzeText(){
	var text = "";
	text = text + "----------------------------------------------------------<BR>";
	text = text + "<BR>";
	text = text + "武将カード別トレード情報<BR>";
	text = text + "----------------------------------------------------------<BR>";

	for( var no in card ){
		text = text + "　" + card[no]['no'] + ". " + card[no]['rare'] + card[no]['name'] + "\t" + formatRightNumber(card[no]['tradecount'],4) + "枚<br />";
		text = text + "　　次期限の入札あり<br />";
		if (card[no]['nextlimit']['bid']['all']['tradecount'] > 0) {
			text = text + "　　　全\t\t" + card[no]['nextlimit']['bid']['all']['tradecount'] + "枚\t最高入札希望価格: " + card[no]['nextlimit']['bid']['all']['maxprice'] + " TP<br />";
			if (card[no]['nextlimit']['bid']['sl0']['tradecount'] > 0) {
				text = text + "　　　スコア・LV0\t" + card[no]['nextlimit']['bid']['sl0']['tradecount'] + "枚\t最高入札希望価格: " + card[no]['nextlimit']['bid']['sl0']['maxprice'] + " TP<br />";
			}
		} else {
			text = text + "　　　該当なし<br />";
		}
		text = text + "　　次期限の入札なし<br />";
		if (card[no]['nextlimit']['nobid']['all']['tradecount'] > 0) {
			text = text + "　　　全\t\t" + card[no]['nextlimit']['nobid']['all']['tradecount'] + "枚\t最低入札希望価格: " + card[no]['nextlimit']['nobid']['all']['minprice'] + " TP<br />";
			if (card[no]['nextlimit']['nobid']['sl0']['tradecount'] > 0) {
				text = text + "　　　スコア・LV0\t" + card[no]['nextlimit']['nobid']['sl0']['tradecount'] + "枚\t最低入札希望価格: " + card[no]['nextlimit']['nobid']['sl0']['minprice'] + " TP<br />";
			}
		} else {
			text = text + "　　　該当なし<br />";
		}
		text = text + "　　期限切れ<br />";
		if (card[no]['nobid']['all']['tradecount'] > 0) {
			text = text + "　　　全\t\t" + card[no]['nobid']['all']['tradecount'] + "枚\t最低入札希望価格: " + card[no]['nobid']['all']['minprice'] + " TP<br />";
			if (card[no]['nobid']['sl0']['tradecount'] > 0) {
				text = text + "　　　スコア・LV0\t" + card[no]['nobid']['sl0']['tradecount'] + "枚\t最低入札希望価格: " + card[no]['nobid']['sl0']['minprice'] + " TP<br />";
			}
		} else {
			text = text + "　　　該当なし<br />";
		}
	}
	text = text + "----------------------------------------------------------<BR>";
	return text;
}

//--------------//
// 分析テキスト //
//-------------//
function makeAnalyzeTsv(){
	var text = "";

	text = text + "no\tレアリティ\t名前\t全枚数\t次期限の入札あり枚数\t次期限の入札あり最高入札希望価格\t次期限の入札ありスコア・LV0枚数\t次期限の入札ありスコア・LV0最高入札希望価格\t次期限の入札なし枚数\t次期限の入札なし最低入札希望価格\t次期限の入札なしスコア・LV0枚数\t次期限の入札なしスコア・LV0最低入札希望価格\t期限切れ枚数\t期限切れ最低入札希望価格\t期限切れスコア・LV0枚数\tスコア・LV0期限切れ最低入札希望価格<br />";
	for( var no in card ){
		text = text + card[no]['no'] + "\t" + card[no]['rare'] + "\t" + card[no]['name'] + "\t" + card[no]['tradecount'] + "\t";
		if (card[no]['nextlimit']['bid']['all']['tradecount'] > 0) {
			text = text + card[no]['nextlimit']['bid']['all']['tradecount'] + "\t" + card[no]['nextlimit']['bid']['all']['maxprice'] + "\t";
			if (card[no]['nextlimit']['bid']['sl0']['tradecount'] > 0) {
				text = text + card[no]['nextlimit']['bid']['sl0']['tradecount'] + "\t" + card[no]['nextlimit']['bid']['sl0']['maxprice'] + "\t";
			} else {
				text = text + "0\t\t";
			}
		} else {
			text = text + "0\t\t0\t\t";
		}
		if (card[no]['nextlimit']['nobid']['all']['tradecount'] > 0) {
			text = text + card[no]['nextlimit']['nobid']['all']['tradecount'] + "\t" + card[no]['nextlimit']['nobid']['all']['minprice'] + "\t";
			if (card[no]['nextlimit']['nobid']['sl0']['tradecount'] > 0) {
				text = text + card[no]['nextlimit']['nobid']['sl0']['tradecount'] + "\t" + card[no]['nextlimit']['nobid']['sl0']['minprice'] + "\t";
			} else {
				text = text + "0\t\t";
			}
		} else {
			text = text + "0\t\t0\t\t";
		}
		if (card[no]['nobid']['all']['tradecount'] > 0) {
			text = text + card[no]['nobid']['all']['tradecount'] + "\t" + card[no]['nobid']['all']['minprice'] + "\t";
			if (card[no]['nobid']['sl0']['tradecount'] > 0) {
				text = text + card[no]['nobid']['sl0']['tradecount'] + "\t" + card[no]['nobid']['sl0']['minprice'] + "<br />";
			} else {
				text = text + "0\t<br />";
			}
		} else {
			text = text + "0\t\t0\t<br />";
		}
	}
	return text;
}

// 小数桁数フォーマッタ
function fm_num(str){
	var temp;
	temp = String(str);

	if( temp.indexOf(".") < 0 ){
		temp = temp + ".00";
	}
	if( temp.indexOf(".") == 1 ){
		temp = "  " + temp;
	}
	if( temp.indexOf(".") == 2 ){
		temp = " " + temp;
	}
	if( temp.length != 6 ){
		temp = temp + "0";
	}

	return temp;
}

// カード枚数から比率を求める
function calc_per(num,base){
	return fm_num(Math.floor(num/base*10000)/100) + "%";
}

// 桁数整形
function formatRightNumber( num, length ){
	var fix = '    ';
	var str;
	var result = '';

	str = num.toString(10);
	if( str.length < length ){
		result = fix.substr(0,length - str.length) + str;
	}
	else{
		result = str;
	}

	return result;
}

function getDateString(addDay) {
	var date = new Date();
	if (addDay) {
		date = new Date(date.getTime() + addDay * 24 * 60 * 60 * 1000);
	}
	var month = (date.getMonth() + 1);
	var day = date.getDate();
	return date.getFullYear().toString() + "-" + (month < 10? '0': '') + month.toString() + "-" + (day < 10? '0': '') + day.toString() + " 10:00";
}
